/**
 * Copyright Spectra, Inc. All Rights Reserved.
 */
package spectra.ee.web.common.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import spectra.base.BaseConfig;
import spectra.ee.web.WebConfig;

/**
 * 환경변수 road/rebuild 한다.
 *
 * @author kwlee
 */
public class ConfigBaseServlet extends HttpServlet
{

    /** 도메인ID. */
    protected String domainId;

    /** 상태 유효기간. (단위: 초) */
    protected long validTerm;

    /**
     * 상태 유효기간을 설정한 후, LoadConfig메소드를 실행한다.
     *
     * @throws ServletException the servlet exception
     **/
    public void init() throws ServletException
    {
    	String loggerConf = getServletContext().getInitParameter("LOGGER_CONF");    	
    	
    	if (loggerConf != null && loggerConf.startsWith("${ENOMIX_HOME}"))
    	{
    		String enomixHome = System.getProperty("ENOMIX_HOME");
    		if (enomixHome != null)
    		{
    			// ${ENOMIX_HOME}을 가져오지 못하는 경우 System.getProperty("ENOMIX_HOME")으로 변경
    			enomixHome = enomixHome.replaceAll("\\\\", "\\/"); // \ ==> / 으로 변경
    			BaseConfig.setLoggerConf(loggerConf.replaceAll("[$]\\{ENOMIX_HOME\\}", enomixHome)); 
    		}
    		else
    		{
    			BaseConfig.setLoggerConf(loggerConf);
    		}
    	}
    	else
    	{
    		BaseConfig.setLoggerConf(loggerConf);
    	}
    	
        this.domainId = getInitParameter("domainId");

        if(this.domainId == null)
        {
            throw new ServletException("domainId is null.");
        }

        try
        {
            this.validTerm = Long.parseLong(getInitParameter("validTerm"));
        }
        catch(Exception e)
        {
            this.validTerm = 30;
        }


        loadConfig();
    }

    /**
     * config값을 rebuild한다.
     */
    protected void loadConfig()
    {
        WebConfig.init(this.domainId, this.validTerm);
        WebConfig.rebuild();
    }
}
